package ArrayList;

import java.util.ArrayList;
import java.util.Random;

//创建一个类 表示玩家 ， 类中用 ArrayList 创建了一个 Card 类型的数组
class handCard{
    private ArrayList<Card> cards;
}

//创建一个牌的类
class Card{
    private String suit;
    private String rank;
    //构造方法 ， 实例化传入的值可直接被接收
    public Card(String suit,String rank){
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }
    public String toString(){
        return '[' +
                "suit = '" +suit +'\'' +
                "rank = '" +rank + '\'' +
                ']';
    }
}

public class Demo01 {
    public static void main(String[] args) {
        //创建一副牌
        ArrayList<Card> deck = createDeck();
        System.out.println("原始的牌：");
        System.out.println(deck);
        shuffle(deck);
        System.out.println("洗牌后：");
        System.out.println(deck);
        ArrayList<ArrayList<Card>> hands = new ArrayList<>();
        for (int k = 0; k < 3; k++) {
            ArrayList<Card> hand = new ArrayList<>();
            hands.add(hand);
        }
        for (int k = 0; k < 5; k++) {
            for (int l = 0; l < 3; l++) {
                ArrayList currenH = hands.get(l);
                Card card = deck.remove(0);
                currenH.add(card);
            }
        }

        //打印玩家的手牌
        for (int k = 0; k < 3; k++) {
            System.out.println("第" + k + "个人的牌为 ：" + hands.get(k));
        }
    }

    //自己实现洗牌的操作
    public static void shuffle(ArrayList<Card> deck) {
        Random random = new Random();
        for (int i = deck.size() - 1; i > 0; i--) {
            int j = random.nextInt(deck.size());
            Card temp = new Card(deck.get(i).getSuit(),deck.get(i).getRank());
            deck.set(i,deck.get(j));
            deck.set(j,temp);


        }
    }

    //创建一副牌的函数
    public static ArrayList<Card> createDeck(){
        ArrayList<Card> deck = new ArrayList<>();
        String[] suits = {"♠", "♥", "♦", "♣"};
        for(String suit : suits){
            //2~10
            for (int i = 2; i <= 10 ; i++) {
                Card card = new Card(suit,""+i);
                deck.add(card);
            }
            //J Q K A
            Card cardJ = new Card(suit , "J");
            Card cardQ = new Card(suit , "Q");
            Card cardK = new Card(suit , "K");
            Card cardA = new Card(suit , "A");
            deck.add(cardJ);
            deck.add(cardQ);
            deck.add(cardK);
            deck.add(cardA);
        }
        return deck;
    }
}


